/**
 * 选择单一地址位置。
 * 常用于地址位置分享。
 */
var ShareLocDialog = (function() {
    var _dialogTmp = [
            '<div class="map-wrapper-google" />'
        ].join(''),
        //详细地址展示模板.
        _locDetailTmp = [
            '<div class="locDetailPanel">',
                '<div class="locName" style="padding-bottom:5px;"></div>',
                '<div style="text-align:right;">',
                    '<button class="button shareLocBtn" lan="location.shareLocTitle"></button>',
                '</div>',
            '</div>'
        ].join(''),        
        /**
         * 默认配置.
         */
        _defConfig = {
            zoom : 16,
            defLocation : {
                latitude : 40,
                longitude : 40
            }
        };
        
    /**
     * 构造函数.
     */
    var _selectLocDialog = function(config) {
        var _this = this;         
        this.config = $.extend(true, {}, _defConfig, config);  
        this.isEditable = this.config['mode'] == 'edit';
        
        this.setProps({
            title : _this.isEditable ? lan('location.shareLocTitle') : lan('location.map'),
            size : {
                width : 1020,
                height : 625
            },
        });
        
        this.addDialogContent(_dialogTmp, function($dialogContent) {
            _this.$mapWrapper = $dialogContent.closest('.map-wrapper-google');
            _this.on('inital', function(){
                ujs.subjects.map.GoogleMapAPI.load(
                    function(){
                        _this.showMask('loading');     
                        if(_this.config['loc']){
                            _this.initLocation(_this.config['loc']);
                        } else {
                            ujs.browser.geo.getCurrentPosition(
                                function(position){
                                    _this.initLocation(position.coords);
                                }, 
                                function(){                        
                                    bubble.msg(lan('location.getLocFailedTip'));
                                    _this.initLocation(_this.config['defLocation']);
                                }
                            );    
                        }
                    }, 
                    _this.$dialogContent
                );    
            });             
        });

    }.extend(ujs.ui.Dialog);
    
    /**
     * @param pos {Mixed} 位置对象，包含latitude及longitude属性。
     */
    _selectLocDialog.prototype.initLocation = function(pos){
        var _this = this,
            latlon = new google.maps.LatLng(pos['latitude'], pos['longitude']);
        
        var map = _this.map = new google.maps.Map(
            _this.$mapWrapper.get(0),
            $.extend({}, ujs.subjects.map.GoogleMapAPI.mapDefConfig, {
                center : latlon,
                zoom : _this.config['zoom']
            })
        );
        
        google.maps.event.addListener(map, 'idle', function() {
            _this.hideMask();
        });
        
        if(_this.isEditable){
            google.maps.event.addListener(map, "click", function(event) {            
                _this.markSelectedLocation(event);
            });        
            google.maps.event.addListener(map, "rightclick", function(event) {            
                _this.markSelectedLocation(event);
            });
        }

        _this.on('fullscreenChanged', function(){
            google.maps.event.trigger(map, 'resize');           
        });        
        //搜索框
        if(_this.isEditable){
            ujs.subjects.map.GoogleMapAPI.util.addSeachBox(map, {
                cls : 'pac-input controls',            
                placeholder : lan('location.searchLocTip')
            }, {
                scaledSize : new google.maps.Size(25, 25)
            }, {
                inital : function(place){
                    this.placeName = place['formatted_address'];
                    _this.createLocDetailPanel(this);
                }
            });
        }        
        //初始位置.
        var marker = new google.maps.Marker({
            position : latlon,
            map : map
        });  
        ujs.subjects.map.GoogleMapAPI.util.getPlaces(latlon, function(addrs, addr){
            if(_this.isEditable){
                marker.placeName = addr;
                _this.createLocDetailPanel(marker);
             }
        }); 
    };
        
    _selectLocDialog.prototype.markSelectedLocation = function(event){    
        var _this = this;        
        if (_this.marker) {
            _this.marker.setVisible(false);
        }
        var marker = new google.maps.Marker({
            map : _this.map,
            icon : {
                url : ujs.getRes('app/im/images/marker/16.png'),
                size : new google.maps.Size(16, 16),
                origin : new google.maps.Point(0, 0)
            },
            title : '',
            position : event.latLng
        });
        _this.marker = marker;
        
        _this.showMask('loading');
        ujs.subjects.map.GoogleMapAPI.util.getPlaces(
            event.latLng, 
            function(addrs, addr) {
                marker.placeName = addr;
                _this.createLocDetailPanel(marker);
                _this.hideMask();
            },
            function(status){
                bubble.msg(status);
                _this.hideMask();
            }
        );
    };
    
    _selectLocDialog.prototype.createLocDetailPanel = function(marker){
        var _this = this,       
            $locDetail = $(_locDetailTmp); 
        lan.parse($locDetail);
         
        $locDetail.find('.locName').text(marker.placeName);        
        $locDetail.find('.shareLocBtn').click(function(){            
            _this.fire('share', marker.position);
        });        
         
        if(_this.infoWindow){
            _this.infoWindow.close();
        }
        _this.infoWindow = new google.maps.InfoWindow({
            content: $locDetail.get(0)
        });
        _this.infoWindow.open(_this.map, marker);
        google.maps.event.addListener(marker, 'click', function() {
            _this.infoWindow.open(_this.map, marker);
        });
    };

    return _selectLocDialog;

})(); 